package com.ctrip.demo.stack;


/**
 * 使用栈实现一个计算器
 */
public class Calculator {

    public static void main(String[] args) {

        String expression = "3-40/4-1";
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operaStack = new ArrayStack(10);
        int num1 = 0, num2 = 0, res = 0, opera;
        char ch = ' ';
        int index = 0;

        String keyNum = "";
        // 解析表达式开始
        while (true) {
            // 逐个拆解表达式
            ch = expression.substring(index, index + 1).charAt(0);
            // 判断是否是运算符
            if (numStack.isOper(ch)) {
                // 判断运算符是否为空，如果不为空则比较运算符的先后顺序，如果优先级小于栈中的优先级，
                // 则popnumstack中的数据记性运算，然后再把数据压入数据栈中
                if (!operaStack.isEmpty()) {
                    if (operaStack.priority(ch) <= operaStack.priority(operaStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        opera = operaStack.pop();
                        res = numStack.cal(num1, num2, opera);
                        numStack.push(res);
                        operaStack.push(ch);
                    } else {
                        // 直接入栈
                        operaStack.push(ch);
                    }
                } else {
                    // 直接入栈
                    operaStack.push(ch);
                }
            } else {
                keyNum += ch;

                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keyNum));
                } else {
                    if (operaStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keyNum));
                        keyNum = "";
                    }
                }
            }

            index ++;
            if (index >= expression.length()) {
                break;
            }
        }

        // 表达式解析完成后，栈中的数据按照一定顺序计算
        while (true) {
            if (operaStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            opera = operaStack.pop();
            res = numStack.cal(num1, num2, opera);
            numStack.push(res);
        }

        int res2 = numStack.pop();
        System.out.printf("[%s]=%d \n", expression, res2);

        System.out.println(3-40/4-1);
    }

}

class ArrayStack {

    private int maxSize;

    private int top = -1;

    private int[] arr;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int obj) {
        if (isFull()) {
            System.out.println("栈满了");
            return;
        }
        arr[++top] = obj;
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空的");
            throw new RuntimeException("栈空的");
        }
        return arr[top--];
    }

    public int peek() {
        if (isEmpty()) {
            System.out.println("栈空的");
            throw new RuntimeException("栈空的");
        }
        return arr[top];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈空的");
            throw new RuntimeException("栈空的");
        }

        for (int i = top; i > 0; i--) {
            System.out.printf("stack[%d]=%d", i, arr[i]);
        }

    }

    public boolean isOper(int obj) {
        return obj == '+' || obj == '-' || obj == '*' || obj == '/';
    }

    public int priority(int opera) {
        if (opera == '*' || opera == '/') {
            return 1;
        } else if (opera == '-' || opera == '+') {
            return 0;
        } else {
            return -1;
        }
    }

    public int cal(int num1, int num2, int opera) {
        int res = 0;
        switch (opera) {
            case '*':
                res = num1 * num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            case '+':
                res = num1 + num2;
                break;
            default:
                break;
        }
        return res;
    }

}